﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using LY.Entity;
using LY.SSOServer.Models;

namespace LY.SSOServer.Services
{
    /// <summary>
    /// TokenService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class TokenService : System.Web.Services.WebService
    {
        /// <summary>
        /// 根据令牌获取用户凭证
        /// </summary>
        /// <param name="tokenValue">令牌</param>
        /// <returns></returns>
        [WebMethod]
        public LY.Entity.U_Member TokenGetCredence(string tokenValue)
        {
            LY.Entity.U_Member o = null; 
            DataTable dt =CacheManager.GetCacheTable(); 
            var list = new List<TokenList>();  
            if (dt != null)
            { 
                list.AddRange(from DataRow row in dt.Rows select row.ConvertToEntity<TokenList>());
            }
           //var firstOrDefault = list.Where(c => c.token == tokenValue && c.mac == CacheManager.GetRemoteMac()).OrderByDescending(c=>c.timeout).FirstOrDefault();
            var firstOrDefault = list.Where(c => c.token == tokenValue).OrderByDescending(c=>c.timeout).FirstOrDefault();
            if (firstOrDefault != null)
                o = firstOrDefault.info; 

            return o;
        }

        [WebMethod]
        public LY.Entity.U_Member TokenGetCredence2(string tokenValue,string ip)
        {
            LY.Entity.U_Member o = null;
            DataTable dt = CacheManager.GetCacheTable();
            var list = new List<TokenList>();
            if (dt != null)
            {
                list.AddRange(from DataRow row in dt.Rows select row.ConvertToEntity<TokenList>());
            }
            //var firstOrDefault = list.Where(c => c.token == tokenValue && c.mac == ip).OrderByDescending(c => c.timeout).FirstOrDefault();
            var firstOrDefault = list.Where(c => c.token == tokenValue ).OrderByDescending(c => c.timeout).FirstOrDefault();
            if (firstOrDefault != null)
                o = firstOrDefault.info;
            else
            {
                ClearToken(tokenValue);
            } 
            return o;
        }

        /// <summary>
        /// 清除令牌
        /// </summary>
        /// <param name="tokenValue">令牌</param>
        [WebMethod]
        public void ClearToken(string tokenValue)
        {
            DataTable dt =  CacheManager.GetCacheTable();
            if (dt != null)
            {
                DataRow[] dr = dt.Select("token = '" + tokenValue + "'");
                if (dr.Length > 0)
                {
                    dt.Rows.Remove(dr[0]);
                }
            }
        }

    }//end class
}
